package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.UpdateArticleMessage;
import com.heima.behavior.service.ILikeService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.MediaThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.Date;

@Service
public class LikeServiceImpl implements ILikeService {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 给文章点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveLike(LikesBehaviorDto dto) {
        //创建点赞类
        ApLikesBehavior likesBehavior=new ApLikesBehavior();
        //如果用户没有登陆,存入用户id
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            likesBehavior.setUserId(user.getUserId());
        }else {
            //没有登陆,存入手机设备名称
            likesBehavior.setEquipmentId(dto.getEquipmentId());
        }
        likesBehavior.setArticleId(dto.getArticleId());
        likesBehavior.setType(0);
        likesBehavior.setOperation(dto.getOperation());
        likesBehavior.setCreatedTime(new Date());
        //查询用户是否点赞过
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if (user==null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else {
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        //如果没有点赞,新增点赞数据 保存
        if (one==null) {
            if (dto.getOperation() == 0) {
                mongoTemplate.save(likesBehavior);
                sendMessage(dto);
            }
            //已经点赞过,更改点赞状态
        }else {
         if (one.getOperation() !=dto.getOperation()){
             one.setOperation(dto.getOperation());
             mongoTemplate.save(one);
             sendMessage(dto);
         }
        }
        return ResponseResult.okResult();
    }

    /**
     * 查询点赞记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<ApLikesBehavior> getLikes(LikesBehaviorDto dto) {
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));

        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        return ResponseResult.okResult(one);

    }

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Value("${topic.like_behavior}")
    private String LikeTopic;

    /**
     * 发信息到kafka
     * @param dto
     */
    public void sendMessage(LikesBehaviorDto dto){
        UpdateArticleMessage message = new UpdateArticleMessage();
        message.setType(1);
        message.setArticleId(dto.getArticleId());
        message.setAdd(1);
        if (dto.getOperation()==1){
            message.setAdd(-1);
        }
        String value = JSON.toJSONString(message);
        kafkaTemplate.send(LikeTopic,value);
    }
}
